package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.DictDao;
import com.atguigu.dao.HouseDao;
import com.atguigu.entity.House;
import com.atguigu.service.HouseService;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;

/**
 * @author:ahang
 * @create:2022-07-25 18:10
 * @Description:
 */
@Service(interfaceClass = HouseService.class)
public class HouseServiceImpl extends BaseServiceImpl<House> implements HouseService {

    @Autowired
    private HouseDao houseDao;

    @Autowired
    private DictDao dictDao;

    @Override
    protected BaseDao<House> getEntityDao() {
        return houseDao;
    }

    @Override
    public void publish(Long id, int status) {
        houseDao.publish(id,status);
    }

    @Override
    public PageInfo<HouseVo> findListPage(Integer pageNum, Integer pageSize, HouseQueryVo houseQueryVo) {
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //查询指定页的数据
        Page<HouseVo> page =  houseDao.findListPage(houseQueryVo);
        //根据字典id查询字典信息name
        List<HouseVo> houseVoList = page.getResult();
        for (HouseVo houseVo : houseVoList) {
            String directionName = dictDao.getNameById(houseVo.getDirectionId());
            houseVo.setDirectionName(directionName);
            String floorName = dictDao.getNameById(houseVo.getFloorId());
            houseVo.setFloorName(floorName);
            String houseTypeName = dictDao.getNameById(houseVo.getHouseTypeId());
            houseVo.setHouseTypeName(houseTypeName);
        }
        //返回数据
        return new PageInfo<>(page);
    }

    @Override
    public House getById(Serializable id) {
        //查询指定id的房源信息
        House house = super.getById(id);
        //根据id获取name 房屋户型 所在楼层 建筑结构 房屋朝向 装修情况 房屋用途
        String houseTypeName = dictDao.getNameById(house.getHouseTypeId());
        house.setHouseTypeName(houseTypeName);
        String floorName = dictDao.getNameById(house.getFloorId());
        house.setFloorName(floorName);
        String BuildStructureName = dictDao.getNameById(house.getBuildStructureId());
        house.setBuildStructureName(BuildStructureName);
        String DirectionName = dictDao.getNameById(house.getDirectionId());
        house.setDirectionName(DirectionName);
        String decorationName = dictDao.getNameById(house.getDecorationId());
        house.setDecorationName(decorationName);
        String houseUseName = dictDao.getNameById(house.getHouseUseId());
        house.setHouseUseName(houseUseName);

        return house;

    }
}
